<template>
  <div class="main-wrapper">
    <Layout>
      <!-- 页面导航标题 -->
      <Header class="main-header">
        <Menu :active-name="activeName" style="max-width: 1280px; margin: 0 auto;" @on-select="handleSelect" v-show="windowSize !== 'xs' && windowSize !== 'sm'" class="home-header-menu" mode="horizontal" theme="light">
          <div class="main-header-logo">
            <h2><a>{{logo}}</a></h2>
          </div>
          <div class="main-header-nav">
            <template v-for="item in menus">
              <SiderSubmenu
                v-if="item.children"
                :key="`menu_${item.name}`"
                :name="item.name"
                :parent="item"
              >
              </SiderSubmenu>
              <MenuItem v-else :key="`menu_${item.name}`" :name="item.name">
                <Icon :type="item.icon" :size="23" />
                {{item.title}}
              </MenuItem>
            </template>
          </div>
        </Menu>
        <div class="main-header-phone" v-show="windowSize === 'xs' || windowSize === 'sm'">
          <div class="main-header-phone-logo">
            <h3><a>{{logo}}</a></h3>
          </div>
          <div class="main-header-phone-menu">
            <Icon @click.native="handleMobileCollapsed" class="phone-menu-icon" type="md-menu" :size="23" />
          </div>

          <!-- 手机模式展开菜单 -->
          <Drawer class="phone-drawer" :styles="{padding: 0}" :closable="false" v-model="mobile" placement="left">
            <Menu :active-name="activeName" theme="light" width="256px" @on-select="handleSelect">
              <template v-for="item in menus">
                <SiderSubmenu
                  v-if="item.children"
                  :key="`menu_${item.name}`"
                  :name="item.name"
                  :parent="item"
                >
                </SiderSubmenu>
                <MenuItem v-else :key="`menu_${item.name}`" :name="item.name">
                  <Icon :type="item.icon" :size="23" />
                  {{item.title}}
                </MenuItem>
              </template>
            </Menu>
          </Drawer>
        </div>
      </Header>
      <!-- 主体内容 -->
      <Content class="main-content">
        <div class="page-content">
          <router-view/>
        </div>
      </Content>
      <!-- 尾部内容 -->
      <!-- <Footer class="main-footer">
        <p>2019-2020 &copy; {{logo}}</p>
      </Footer> -->
    </Layout>
  </div>
</template>

<script>
import SiderSubmenu from '_c/sider-menu/SiderSubmenu.vue'
import {getMenus} from '@/api/menu'

export default {
  name: 'Main',
  components:{
    SiderSubmenu
  },
  data(){
    return {
      fullWidth: document.documentElement.clientWidth,
      fullHeight: document.documentElement.clientHeight,
      mobile: false,
      menus: [],
      activeName: '',
      logo: 'TPSHION 配置中心',
    }
  },
  methods:{
    /**
     * 动态获取浏览器宽高
     */
    handleResize() {
      this.fullWidth = document.documentElement.clientWidth;
      this.fullHeight = document.documentElement.clientHeight;
    },
    /**
     * 控制手机模式菜单展开关闭
     */
    handleMobileCollapsed(){
      this.mobile = !this.mobile
    },
    /**
     * 获取菜单数据
     */
    getMenuData(){
      this.menus = [
        {
          name: 'home',
          title: '首页',
          icon: ''
        },
        {
          name: 'deploy',
          title: '自动化部署',
          icon: '',
        },
        {
          name: 'doc',
          title: '文档',
          icon: '',
        }
      ]
      this.activeName = this.$route.name
    },
    /**
     * 处理菜单选中
     */
    handleSelect(name){
      if(this.mobile) this.mobile = false
      this.activeName = name
      this.$router.push({name: name})
    }
  },
  computed:{
    /**
     * 动态计算浏览器窗口大小
     * xs: '480px',
     * sm: '576px',
     * md: '768px',
     * lg: '992px',
     * xl: '1200px',
     * xxl: '1600px'
     */
    windowSize() {
      if(this.fullWidth >= 0 && this.fullWidth <= 480){
        return 'xs'
      }else if(this.fullWidth > 480 && this.fullWidth <= 576){
        return 'sm'
      }else if(this.fullWidth > 576 && this.fullWidth <= 768){
        return 'md'
      }else if(this.fullWidth > 768 && this.fullWidth <= 992){
        return 'lg'
      }else if(this.fullWidth > 992 && this.fullWidth <= 1200){
        return 'xl'
      }else if(this.fullWidth > 1200){
        return 'xxl'
      }
    }
  },
  created() {
    window.addEventListener("resize", this.handleResize)
    /**
     * 初始化菜单数据
     */
    this.getMenuData()
  },
  beforeDestroy: function () {
    window.removeEventListener("resize", this.handleResize)
  }
}
</script>

<style lang="less" scoped>
.main-wrapper{
  background: #f5f7f9;
  position: relative;
  border-radius: 4px;
  overflow: hidden;
  .main-header{
    position: fixed!important;
    width: 100%;
    background-color: #fff;
    box-shadow: 0 3px 4px rgb(224 224 224);
    z-index: 999;
    padding: 0px;
    height: 60px;
    border-bottom: 1px solid #dcdee2;
    .main-header-menu{
      padding: 0px 50px;
      padding-left: 0px;
    }
    .main-header-logo{
      float: left;
      position: relative;
      width: 190px;
    }
    .main-header-nav{
      margin: 0 auto;
      margin-right: 20px;
      float: right;
    }
    .main-header-phone{
      padding: 0px 20px;
      .main-header-phone-logo{
        width: 150px;
        height: 60px;
        line-height: 60px;
        float: left;
      }
      .main-header-phone-menu{
        width: 50px;
        height: 60px;
        line-height: 60px;
        float: right;
        text-align: right;
        .phone-menu-icon{
          line-height: 60px;
        }
      }
    }
  }
  .main-content{
    padding: 0px;
    padding-top: 5px;
    margin-top: 60px;
    min-height: ~"calc(100vh - 139px)";
    .page-content{
      margin: 0 auto;
    }
  }
  .main-footer{
    text-align: center;
    width: 100%;
    height: 79px;
    background-color: #515a6e;
    color: white;
    padding: 10px;
    a{
      color: #fff;
    }
    p{
      line-height: 79px;
    }
  }
}
</style>
